package com.zking.frame.LYK;


import java.awt.Color;
import java.awt.Container;

import java.awt.Cursor;
import java.awt.FlowLayout;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.JTextField;
import javax.swing.WindowConstants;

import com.zking.frame.TJ.mySpace;
import com.zking.frame.TJ.JDBC.GetUserid;
import com.zking.frame.TL.Films;
import com.zking.utils.GetFilmAllData;



public class SelectSeatJframe extends JFrame implements MouseListener{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel p1;
	private JLabel btlist[];
	private List<Integer> seat;

	private ImageIcon selected;
	private ImageIcon unselect;
	private ImageIcon selecting;
	private  JPanel top_p;//上部面板
	private  JLabel logopic;
	private  JLabel city;//选择城市
	private  JLabel homepage;//首页跳转按钮
	private  JLabel film;//影视页面跳转按钮
	private  JLabel cinema;//电影院页面调转按钮
	private  JLabel personData;
	private  JLabel out;
	
	//中层面板
	private JPanel center_top;
	private JLabel center_topl;
	private JLabel center_topc;
	private JLabel center_topr;
	private JLabel center_tops;
	
	//中层右边面板
	
	private JPanel center_right;
	private JLabel filmMSg;
	private JLabel cinemaName;
	private JLabel yingting;
	private JLabel portset;
	private JLabel seatMsg;
	private JLabel seatNumber;
	private JLabel ticketPrice;
	private JLabel phone;
	private JTextField phoneMsg;
	private JButton ensure;
	
	
	private JPanel center_center;
	private JLabel sgreen ;
	 
	
	private JLabel seatt;
	
	private JLabel saleMsg; 
	private JLabel bottomP;
	private JLabel bottomP2;
	int isSelect[];
	int sum;
	Container c = getContentPane();
	private StringBuffer s = new StringBuffer();
	private String film_name;
	Map<String ,Object> map;
	public SelectSeatJframe(String film_namern ,String cinemaN,String ting) {
		film_name=film_namern;
		map=GetFilmAllData.getFilmDataByName(film_namern);
		GetUser uMsg=new GetUser();
		 sum=0;
		isSelect=new int[100];
		

		c.setLayout(null);//绝对布局，自己设定位置
		c.setBackground(Color.WHITE);
	

	
		 top_p=new JPanel();
		 top_p.setBounds(0,0,1842,90);
		 top_p.setLayout(null);
		 top_p.setBackground(Color.WHITE);
		 c.add(top_p);
		 //首页logo图片
		 ImageIcon logo=new ImageIcon("imges/logo.png");
		 
		 logopic=new JLabel(logo);
		 logopic.setBounds(60,0,logo.getIconWidth(),logo.getIconHeight());
		 top_p.add(logopic);
		 
		 //首页城市选择
		 city=new JLabel("湘潭") ;
		 city.setBounds(250, 10, 70, 40);
		 
		 city.setFont(new Font("楷体", Font.CENTER_BASELINE, 30));
		 top_p.add(city);
		 
		 
		 //首页按钮
		 homepage=new JLabel("首页");
		 homepage.setBounds(360, 10, 70, 40);
		 homepage.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 homepage.addMouseListener(this);
		 top_p.add(homepage);
		 
		 //回到影视页面按钮
		 film=new JLabel("影片");
		 film.setBounds(480, 10, 70, 40);
		 film.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 film.addMouseListener(this);
		 top_p.add(film);
		 //回到影院的按钮
		 cinema=new JLabel("影院");
		 cinema.setBounds(590, 10, 70, 40);
		 cinema.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 cinema.addMouseListener(this);
		 top_p.add(cinema);
		 //个人中心
		 
		 
		 personData=new JLabel("个人中心 |");
		 personData.setBounds(1230, 10, 120, 40);
		 personData.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 personData.addMouseListener(this);
		 top_p.add(personData);
		 
		 //退出
		 out=new JLabel("退出");
		 out.setBounds(1350, 10, 70, 40);
		 out.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 out.addMouseListener(this);
		 top_p.add(out);
		 
		 
		 
		 center_top=new JPanel();
		 center_top.setLayout(new GridLayout(1,4,0,0));
		 center_top.setBounds(150, 100, 1300, 40);
		 center_top.setBackground(Color.WHITE);
		 c.add(center_top);
		 
		 center_topl=new JLabel("1.选择影片、场次");
		 center_topl.setForeground(Color.GREEN);
		 center_topl.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 15));
		 center_top.add(center_topl);
		 
		 center_topc=new JLabel("2.选座、填手机号");
		 center_topc.setForeground(Color.RED);
		 center_topc.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 15));
		 center_top.add(center_topc);
		 
		 
		 center_topr=new JLabel("3.确认订单、支付");
		 center_topr.setForeground(Color.GRAY);
		 center_topr.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 15));
		 center_top.add(center_topr);
		 
		 
		 center_tops=new JLabel("4.支付成功、影院取票观影");
		 center_tops.setForeground(Color.GRAY);
		 center_tops.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 15));
		 center_top.add(center_tops);
		 
		 center_center=new JPanel();
		 center_center.setBackground(Color.WHITE);
		 center_center.setBounds(150, 145, 700,70);
		 center_center.setLayout(new GridLayout(1,1,0,0));
		 
		 c.add(center_center);
		 ImageIcon pingmu=new ImageIcon("imges/screen.png");
		 
		 sgreen=new JLabel(pingmu);
		 center_center.add(sgreen);
		 
		 
		 center_right=new JPanel();
		 center_right.setBackground(Color.WHITE);
		 center_right.setBounds(900, 155, 320,600);
		 center_right.setLayout(new GridLayout(11,1,0,0));
		 c.add(center_right);
		 
		
		
	
		 
		 filmMSg=new JLabel("电影名："+film_namern +" \t国语 2D");
		 filmMSg.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(filmMSg);
		 
		 cinemaName=new JLabel("影院："+cinemaN);
		 cinemaName.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(cinemaName);
		 
		 yingting=new JLabel("影厅："+ting);
		 yingting.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(yingting);
		 Date date=new Date();
		 int  month=date.getMonth()+1;
		 int day=date.getDate();
		 portset=new JLabel("场次："+month+"/"+day+"  下午13:30");
		 portset.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(portset);
		 
		 seatMsg=new JLabel("座位：");
		 seatMsg.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(seatMsg);
		 
		 seatNumber=new JLabel("你已选择0个座位，点击取消重新选座");
		 seatNumber.setFont(new Font("楷体", Font.CENTER_BASELINE, 15));
		 center_right.add(seatNumber);
		 
		 ticketPrice=new JLabel("总价：￥");
		 ticketPrice.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(ticketPrice);
		 
		 phone=new JLabel("接收电子码手机号（11位）");
		 phone.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(phone);
		 //
		 phoneMsg=new JTextField(""+uMsg.getUserMsg().get(2));
		 phoneMsg.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 center_right.add(phoneMsg);
		 
		 ensure=new JButton("确认信息 、下单");
		 ensure.setFont(new Font("楷体", Font.CENTER_BASELINE, 20));
		 ensure.setBackground(Color.RED);
		 center_right.add(ensure);
		 ensure.addMouseListener(this);
		 
		 
		 
		 
		 
		 
		 
		 
		
		 
		seat=new ArrayList<Integer>();
		btlist=new JLabel[100];
		p1=new JPanel(); 
		p1.setBackground(Color.WHITE);
		p1.setBounds(275, 225, 450,310);
		
		p1.setLayout(new GridLayout(10, 10, 0, 3));
		c.add(p1);
		
		selecting=new ImageIcon("imges/selecting.png");
		
		unselect=new ImageIcon("imges/unselect.png");
		selected=new ImageIcon("imges/selected.png");
		
		for (int i = 0; i < btlist.length; i++) {
			btlist[i]=new JLabel(unselect);
		
			btlist[i].addMouseListener(this);
			p1.add(btlist[i]);
		}
		ImageIcon tishi=new ImageIcon("imges/tishi.png");
		
		seatt=new JLabel(tishi);
		seatt.setBounds(150, 550, 700, 60);
		c.add(seatt);
		
		saleMsg=new JLabel("本厅共有100个座位，已售0个");
		saleMsg.setBounds(400, 610, 250, 30);
		c.add(saleMsg);
		
		ImageIcon bottompic=new ImageIcon("imges/bottompic.png");
		
		bottomP=new JLabel(bottompic);
		bottomP.setBounds(150, 640, 700, 80);
		c.add(bottomP);
		
		ImageIcon bottompic2=new ImageIcon("imges/bottompic2.png");
		bottomP2=new JLabel(bottompic2);
		bottomP2.setBounds(0, 770, 1482, 200);
		c.add(bottomP2);
		
		getselectNumber se=new getselectNumber();
		List<Integer> list=se.getNumber(cinemaN,film_namern);
		for (int i = 0; i < list.size(); i++) {
			btlist[list.get(i)].setIcon(selected);
			isSelect[list.get(i)]=2;
			
		}
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);//点击关闭按钮退出
		setSize(1500, 1000);//尺寸
		setLocationRelativeTo(null);//居中
		setVisible(true);//可见		
		
	}


	
	


	//选座后按钮变为红色
	
	

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj=e.getSource();
		for (int i = 0; i < btlist.length; i++) {
			
			if(obj==btlist[i]){
				if(isSelect[i]==0){
					isSelect[i]=1;
					btlist[i].setIcon(selecting);
					sum++;
					saleMsg.setText("本厅共有100个座位，已售"+sum+"个");
					seatNumber.setText("你已选择"+sum+"个座位，点击取消重新选座");
					int x=(i+1)%10;
					int y=i/10+1;
					if(x==0)
					{
						x=10;
					}
					s.append(y+"排"+x+"座"+",");
					seatMsg.setText("座位："+s);
					ticketPrice.setText("总价："+(sum*33)+"￥");
					center_topc.setForeground(Color.GREEN);
					
				}
				else if(isSelect[i]==1){
					isSelect[i]=0;
					btlist[i].setIcon(unselect);
					sum--;
					saleMsg.setText("本厅共有100个座位，已售"+sum+"个");
					seatNumber.setText("你已选择"+sum+"个座位，点击取消重新选座");
					ticketPrice.setText("总价："+(sum*33)+"￥");
					s.setLength(0);
					seatMsg.setText("座位："+s);
					for (int j = 0; j < isSelect.length; j++) {
						
						if(isSelect[j]==1){
							int x=(j+1)%10;
							int y=j/10+1;
							if(x==0)
							{
								x=10;
							}
							
							s.append(y+"排"+x+"座"+",");
							seatMsg.setText("座位："+s);
						}
					}
					
				}
			
					
			
			
			}
			
			
		}
		if(obj==ensure){
			center_topr.setForeground(Color.GREEN);
			if(phoneMsg.getText()!=""||phoneMsg.getText().length()!=0){
				//JOptionPane.showMessageDialog(c, "请输入电话号码");
				String nidaye[]=seatMsg.getText().substring(3).split(",");
				
				int i=0;
				
				for (int j = 0; j < isSelect.length; j++) {
					if(isSelect[j]==1){
						
						
						String seat_all=nidaye[i];
						int user_id=GetUserid.getId();
						int seatnumber=j;
						int scenei_id=1;
						int price=33;
						i++;
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
						String nowtime=df.format(new Date());
						String cinema_hall=cinemaName.getText().substring(3)+"-"+portset.getText().substring(3);
						String film_src=map.get("图片")+"";
				
						InsertSeatdata.insertSAll(seat_all, user_id, seatnumber, scenei_id, price,film_name,cinemaName.getText().substring(3));
						 String sql="INSERT INTO `lyk`.`orderAll` (`order_id`, `user_id`, `film_name`,  `seat`, `price`,`time`,`cinema_hall`,"
						 		+ "`film_src`,`order_state`) VALUES (NULL," +"'"+GetUserid.getId()+"'"+",'"+film_name+"',"+"'"+seat_all+"',"+"'"+"33"+"','"
								 +nowtime+"','"+cinema_hall+"','"+film_src+"','待付款'"+")";
						 Connection conn=JDBC.getConn();
						Statement st;
						try {
							st = conn.createStatement();
							int z=st.executeUpdate(sql);	
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
			
								 
					}
//				new InsertSeatdata(seat_all, user_id, seatnumber, scenei_id, price)
				}
				
				dispose(); 
				new EnsureOrderJFrame(film_name, cinemaName.getText(), yingting.getText().substring(3), portset.getText().substring(3), sum, seatMsg.getText().substring(3));
			}
			
			
		}
		if(obj==film) {
			homepage.setForeground(Color.RED);
			film.setForeground(Color.RED);
			dispose();
			new Films();
			
		}
		
		if(obj==cinema) {
			
		}
		if(obj==personData) {
			dispose();
			new mySpace();
		}
		if(obj==out) {
			dispose();
		}
		if(obj==homepage) {
			dispose();
			new HomepageJframe();
		}
	}



	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		Object obj=e.getSource();
		if(obj==homepage) {
			
			homepage.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移入
		}
		if(obj==film) {
			film.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移入
			}
		if(obj==cinema) {
			cinema.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移入
		
		}
		if(obj==personData) {
			personData.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移入
			
		}
		if(obj==out) {
			out.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//鼠标移入
		
		}
		
	}



	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}

